package com.heima.wemedia.controller;


import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmNewsDto;
import com.heima.model.common.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.entity.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("api/v1/news")
public class WmNewsController {

    @Autowired
    private WmNewsService wmNewsService;
    ///api/v1/news/submit

    @PostMapping("submit")
    public ResponseResult submit(@RequestBody WmNewsDto dto) {
        return wmNewsService.submit(dto);
    }

    /*
    /api/v1/news/list
    WmNewsPageReqDto
     */

    @PostMapping("list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.list(dto);

    }

    /*
    /api/v1/news/list
    WmNewsPageReqDto
     */

    @PostMapping("list_vo")
    public ResponseResult listvo(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.list(dto);

    }

    /**
     * one_vo/{id}
     * 根据id查询文章详情
     */


    @GetMapping("one_vo/{id}")
    public ResponseResult onevo(@PathVariable Long id) {
        return ResponseResult.okResult(wmNewsService.queryById(id));

    }

    ///api/v1/channel/one/{id}

    /**
     * 文章回显用于修改
     *
     * @param id
     * @return
     */
    @GetMapping("one/{id}")
    public ResponseResult edit(@PathVariable Long id) {
        return ResponseResult.okResult(wmNewsService.queryById(id));
    }
    ///api/v1/channel/del_news/{id}

    /**
     * 根据id删除文章
     *
     * @param id
     * @return
     */
    @GetMapping("del_news/{id}")
    public ResponseResult del(@PathVariable Long id) {
        return wmNewsService.del(id);
    }


    //auth_fail

    /**
     * 审核不通过
     * @param dto
     * @return
     */
    @PostMapping("auth_fail")
    public ResponseResult authfail(@RequestBody WmNewsDto dto) {
        WmNews wmNews = new WmNews();

        //  BeanUtils.copyProperties(dto,wmNews);
        wmNews.setId(dto.getId());
        wmNews.setStatus(dto.getStatus());
        wmNews.setReason(dto.getMsg());
        wmNews.setUpdatedTime(new Date());

        return ResponseResult.okResult(wmNewsService.update(wmNews));
    }

    /**
     * 审核通过
     * @param dto
     * @return
     */
    @PostMapping("auth_pass")
    public ResponseResult authpass(@RequestBody WmNewsDto dto) {
        WmNews wmNews = new WmNews();

        //  BeanUtils.copyProperties(dto,wmNews);
        wmNews.setId(dto.getId());
        wmNews.setStatus(dto.getStatus());
        wmNews.setReason(dto.getMsg());
        wmNews.setUpdatedTime(new Date());

        return ResponseResult.okResult(wmNewsService.update(wmNews));
    }
}
